package experiment4.task1;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @author 冯永龙
 * @date 2021/4/5 9:48
 */
public class PersonInput implements PersonAction {
    Scanner sc = new Scanner(System.in);
    private ArrayList<Person> personList = new ArrayList<>();


    public ArrayList<Person> getPersonList() {
        return personList;
    }

    //创建person并将其扔到personList中
    public List<Person> createPerson() {
        String str;
        Person person, newPerson;

        do {
            person = new Person();
            newPerson = process(person);
            personList.add(newPerson);
            System.out.println("输入y继续否则结束：");
            str = sc.next();
        } while (str.equals("y"));

        return personList;
    }

    @Override
    public Person process(Person person) {
        Class<? extends Person> personClass = person.getClass();
        Field[] fields = person.getClass().getDeclaredFields();
        Iterator<Field> iterator = Arrays.stream(fields).iterator();
        while (iterator.hasNext()) {
            Field next = iterator.next();
            Label label = next.getAnnotation(Label.class);
            if (label != null) {
                System.out.print(label.value());
                //获取属性的类型
                Class<?> type = next.getType();
                //获取方法名
                String methodName = "set" + next.getName().substring(0, 1).toUpperCase() + next.getName().substring(1);
                try {
                    Method method = personClass.getMethod(methodName, type);
                    Object var = null;
                    String value = sc.nextLine();
                    String name = type.getName();
                    //将输入的string类型转换为该属性的类型
                    switch (name) {
                        case "java.lang.Integer":
                            var = Integer.parseInt(value);
                            break;
                        case "java.lang.String":
                            var = value;
                            break;
                        case "java.lang.Double":
                            var = Double.parseDouble(value);
                            break;
                        case "java.lang.Boolean":
                            var = Boolean.parseBoolean(value);
                    }
                    method.invoke(person, var);
                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return person;
    }

    @Override
    public String toString() {
        return "PersonInput{" +
                "personList=" + personList +
                '}';
    }

    static class test {
        public static void main(String[] args) {
            PersonInput personInput = new PersonInput();
            personInput.createPerson();
            System.out.println(personInput.toString());
        }
    }
}
